package top.xiaoshuaila.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import top.xiaoshuaila.mapper.MailLogMapper;
import top.xiaoshuaila.pojo.*;
import top.xiaoshuaila.mapper.EmployeeMapper;
import top.xiaoshuaila.service.IEmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 小帅啦
 * @since 2021-05-01
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private MailLogMapper mailLogMapper;

    @Override
    public RespPageBean getEmployeeByPage(Integer currentPage,
                                          Integer size,
                                          Employee employee,
                                          LocalDate[] beginDateScope) {
        Page<Employee> page=new Page<>(currentPage,size);
        IPage<Employee> employeeByPage =
                employeeMapper.getEmployeeByPage(page, employee,
                        beginDateScope);
        RespPageBean respPageBean=
                new RespPageBean(employeeByPage.getTotal(),
                        employeeByPage.getRecords());
        return respPageBean;
    }

    @Override
    public RespBean maxWorkId() {
        List<Map<String, Object>> list =
                employeeMapper.selectMaps(new QueryWrapper<Employee>().select("max(workID)"));
        Integer i=
                Integer.parseInt(list.get(0).get("max(workID)").toString())+1;
        String s=String.valueOf(i);
        return RespBean.success(null,s);
    }

    @Override
    public RespBean addEmp(Employee employee) {
        LocalDate beginContract=employee.getBeginContract();
        LocalDate endContract=employee.getEndContract();
        long days = beginContract.until(endContract,
                ChronoUnit.DAYS);
        DecimalFormat decimalFormat=new DecimalFormat("##.00");
        employee.setContractTerm(Double.parseDouble(decimalFormat.format(days/365.00)));
        if(1==employeeMapper.insert(employee)) {
            Employee emp=
                    employeeMapper.getEmployee(employee.getId()).get(0);
            String msgID= UUID.randomUUID().toString().replace("-","");
            //String msgID="123456";
            MailLog mailLog=new MailLog();
            mailLog.setMsgId(msgID);
            mailLog.setEid(employee.getId());
            mailLog.setStatus(0);
            mailLog.setRouteKey(MailConstants.ROUTING_NAME);
            mailLog.setExchange(MailConstants.EXCHANGE_NAME);
            mailLog.setCount(0);
            mailLog.setTryTime(LocalDateTime.now().plusMinutes(MailConstants.MSG_TIMEOUT));
            mailLog.setCreateTime(LocalDateTime.now());
            mailLog.setUpdateTime(LocalDateTime.now());
            //插入数据库
            mailLogMapper.insert(mailLog);

            //添加员工成功时向mail.welcome管道发送一个employee对象
            rabbitTemplate.convertAndSend(MailConstants.EXCHANGE_NAME,
                    MailConstants.ROUTING_NAME,
                    emp,
                    new CorrelationData(msgID));
            return RespBean.success("添加成功!");
        }
        return RespBean.error("添加失败!");
    }

    @Override
    public List<Employee> getEmployee(Integer id) {
        return employeeMapper.getEmployee(id);
    }

    @Override
    public RespPageBean getEmployeeWithSalary(Integer currenttPage,
                                              Integer size) {
        //创建一个Employee的page对象,设置其页数和大小
        Page<Employee>page=new Page<>(currenttPage,size);
        //将page设为传入参数,IPage<Employee>作为接收参数进行sql语句查询
        IPage<Employee>employeeIPage=
                employeeMapper.getEmployeeWithSalary(page);
        RespPageBean respPageBean=
                new RespPageBean(employeeIPage.getTotal(),
                        employeeIPage.getRecords());
        //最后返回resppageBean对象
        return respPageBean;
    }
}
